package com.project.controller;

import com.project.bean.UserBean;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService service;
    @RequestMapping("login")
    public String login(String userName, String pwd, HttpServletRequest req){
      UserBean user =  service.login(userName,pwd);
      if(user != null){
          req.getSession().setAttribute("logUser",user);
          return "ok";
      }
      return "no";
    }
    @RequestMapping("getUserSession")
    public UserBean getUserSession(HttpServletRequest req){
        return (UserBean) req.getSession().getAttribute("logUser");
    }

    @RequestMapping("add")
    public String add(UserBean user, @RequestParam("pic")MultipartFile mf) throws Exception{
        String fileName = mf.getOriginalFilename();
        fileName = System.currentTimeMillis()+fileName.substring(fileName.lastIndexOf("."));
        mf.transferTo(new File("E:/webtest/CHRYSLER/src/main/resources/static/html/img/"+fileName));
        user.setImg(fileName);
        service.add(user);
        return "ok";
    }
}
